package otw.utils.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Properties util
 * 
 * @author eric
 * @date 2011-2-28 02:18:55
 */
public class PropertiesUtil {
	/**
	 * read properties file, relative to root classpath,
	 * 
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static Properties load(String path) throws IOException {
		Properties ps = new Properties();
		InputStream stream = PropertiesUtil.class.getClassLoader().getResourceAsStream(path);
		ps.load(stream);
		return ps;
	}

	/**
	 * read properties file, relative to specified class,
	 * 
	 * @param clazz
	 * @param path
	 * @return
	 * @throws IOException
	 */
	public static Properties load(Class<?> clazz, String path) throws IOException {
		Properties ps = new Properties();
		InputStream stream = clazz.getResourceAsStream(path);
		ps.load(stream);
		return ps;
	}

	public static void main(String[] args) throws IOException {
		Properties prop = load("eric/j2se/properties/config.properties");
		System.out.println("username:" + prop.get("username"));
		System.out.println("password:" + prop.get("password"));

		Properties prop2 = load(PropertiesUtil.class, "config.properties");
		System.out.println("username:" + prop2.get("username"));
		System.out.println("password:" + prop2.get("password"));
	}
}
